//
//  GameDetailCommentListViewController.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/10/9.
//  Copyright © 2021 myTV. All rights reserved.
//

#import "GameDetailCommentListViewController.h"
#import "GameDetailCommentTableView.h"
#import "GameCommentsListApi.h"
#import "CommentsModel.h"
#import "CustomAlertSheetView.h"

@interface GameDetailCommentListViewController ()

@property (nonatomic,copy) NSString *gameId;

@property (nonatomic, strong) GameDetailCommentTableView *storeTableView;
@property (nonatomic,strong) GameCommentsListApi *listApi;
@property (nonatomic,strong) NSMutableArray *modelList5;/// 评论列表
@property (nonatomic,strong) NSArray *shieldContentList;/// 屏蔽内容列表
@property (nonatomic,strong) NSArray *shieldUserList;/// 屏蔽用户列表

@end

@implementation GameDetailCommentListViewController

- (NSArray *)shieldContentList
{
    if (!_shieldContentList) {
        _shieldContentList = [[NSArray alloc] init];
    }
    return _shieldContentList;
}
- (NSArray *)shieldUserList
{
    if (!_shieldUserList) {
        _shieldUserList = [[NSArray alloc] init];
    }
    return _shieldUserList;
}

- (GameCommentsListApi *)listApi
{
    if (!_listApi) {
        _listApi = [[GameCommentsListApi alloc] initWithGameId:_gameId];
    }
    return _listApi;
}

-(NSMutableArray *)modelList5
{
    if (!_modelList5) {
        _modelList5 = [NSMutableArray array];
    }
    return _modelList5;
}

- (instancetype)initWithGameId:(NSString *)gameId
{
    if (self = [super init]) {
        self.gameId = gameId;
        [self refreshData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviView.hidden = YES;
//    self.view.backgroundColor = [UIColor colorWithHexString:self.colorStr];
    GameDetailCommentTableView *storeTableView = [[GameDetailCommentTableView alloc] init];
    [self.view addSubview:storeTableView];
    [storeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    VHWeakSelf
    storeTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        VHStrongSelf
        self.listApi.pageNum ++;
        [self refreshHandler:^{
            
        }];
    }];
    storeTableView.tableHeaderReloadBlock = ^{
       VHStrongSelf
        [self refreshData];
    };
    storeTableView.tableActionBlock = ^(NSInteger tag, CommentsModel * _Nonnull model) {
        VHStrongSelf
        [self holdbackAction:tag model:model];
    };
    self.storeTableView = storeTableView;
    
}

- (void)refreshData
{
    self.shieldContentList = [LocalData getShieldContentWithGameId:self.gameId];
    self.shieldUserList = [LocalData getShieldUsers];
    self.listApi.pageNum = 1;
    [self refreshHandler:^{
        [self.storeTableView tableHeaderRefreshFinish];
    }];
}


-(void)refreshHandler:(void(^)(void))block
{
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if(self.listApi.success){
            if (self.listApi.pageNum == 1) {
                [self.modelList5 removeAllObjects];
            }
            NSArray *dataArray = [CommentsModel mj_objectArrayWithKeyValuesArray:self.listApi.resArr];
            dataArray = [self checkContentList:dataArray];
            dataArray = [self checkUserList:dataArray];
            [self.modelList5 addObjectsFromArray:dataArray];
            self.storeTableView.modelList = self.modelList5;
        }
        [self tableViewEndRefresh:self.listApi.isMore];
        block();
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self tableViewEndRefresh:self.listApi.isMore];
        block();
    }];
}
    


- (void)tableViewEndRefresh:(BOOL)isMore
{
    if (isMore) {
        [self.storeTableView.mj_footer endRefreshing];
    }else{
        [self.storeTableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.storeTableView.mj_header endRefreshing];
}

- (void)holdbackAction:(NSInteger)tag model:(CommentsModel *)model
{
    if (tag == 0) {
        //举报
        NSArray *tipOffArray = @[@"政治谣言", @"色情低俗", @"恶意扰乱平台秩序", @"冒充他人", @"敲诈勒索", @"骗取钱财", @"其他违规情况"];
        CustomAlertSheetView *alertSheetView = [[CustomAlertSheetView alloc] initWithCustomArr:tipOffArray andSubView:UIDevice.vg_rootWindow];
        [UIDevice.vg_rootWindow addSubview:alertSheetView];
        
        alertSheetView.selectIndex = ^(NSInteger index, NSString *selectText) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [UIDevice.vg_rootWindow makeToast:@"举报成功，客服人员会尽快处理" duration:2 position:CSToastPositionCenter];
            });
        };
    } else if (tag == 1) {
        //屏蔽此内容
        NSLog(@"屏蔽此内容");
        [LocalData saveShieldContentWithGameId:model.gameId commentId:model.commentId];
        NSInteger index = [self.modelList5 indexOfObject:model];
        if (self.modelList5.count > index) {
            [self.modelList5 removeObject:model];
            [self.storeTableView deleteRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    } else if (tag == 2) {
        //屏蔽他的动态
        NSLog(@"屏蔽他的动态");
        UserInfoModel *iosUser = model.iosUser;
        NSDictionary *userInfo = iosUser.mj_JSONObject;
        [LocalData saveShieldUsersWithUserInfoDic:userInfo];
        NSInteger index = [self.modelList5 indexOfObject:model];
        if (self.modelList5.count > index) {
            [self.modelList5 removeObject:model];
            [self.storeTableView deleteRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
}

- (NSMutableArray *)checkContentList:(NSArray *)dataArray
{
    NSMutableArray *array = [NSMutableArray array];
    for (CommentsModel *model in dataArray) {
        BOOL isExist = NO;
        for (NSString *commentId in self.shieldContentList) {
            if ([commentId isEqualToString:model.commentId]) {
                isExist = YES;
                break;
            }
        }
        if (!isExist) {
            [array addObject:model];
        }
    }
    return array;
}
- (NSMutableArray *)checkUserList:(NSArray *)dataArray
{
    NSMutableArray *array = [NSMutableArray array];
    for (CommentsModel *model in dataArray) {
        BOOL isExist = NO;
        for (NSDictionary *userInfo in self.shieldUserList) {
            if ([userInfo[@"useId"] isEqualToString:model.userId]) {
                isExist = YES;
                break;
            }
        }
        if (!isExist) {
            [array addObject:model];
        }
    }
    return array;
}

@end
